package com.woniuxy.handler;

import com.woniuxy.entity.Product;
import com.woniuxy.service.ProductService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 曾令忠
 * @Description:
 * @Date: 2020/7/17 0017-下午 2:31
 */
@RestController
@RequestMapping("/product")
@Api(value="商品接口",tags="ProductApi")
public class ProductHandler {
    @Autowired
    private ProductService productService;

    @GetMapping("/all")
    @ApiOperation(value="查询所有商品",notes="返回所有商品")
    public List<Product> all() {
        return productService.all();
    }

    @GetMapping("/find")
    @ApiOperation(value="以id查询商品",notes="返回商品")
    @ApiImplicitParam(paramType="query",name="pid",value="商品id",required=true)
    public Product findByPid(int pid) {
        return productService.findByPid(pid);
    }

    //以id、名字删除
    @PostMapping("/del")
    @ApiOperation(value="以商品id、商品名删除",notes="返回1表示成功，0表示失败")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    paramType="query",name="pid",value="商品id",
                    dataType="Integer",required=true),
            @ApiImplicitParam(
                    paramType="query",name="pname",value="商品名称",
                    dataType="String",required=true)
    })
    public int delete(int pid,String pname) {
        return productService.deleteByPidAndPname(pid, pname);
    }

    //增加
    @PostMapping("/add")
    @ApiOperation(value="添加商品",notes="返回1表示成功，0表示失败!")
    @ApiResponse(code=400,message="参数不对",response=String.class)
    public int add(Product product) {
        return productService.addProduct(product);
    }

    //修改
    @PutMapping("/update")
    @ApiOperation(value="修改商品",notes="返回1表示成功，0表示失败!")
    public int update(Product product) {
        return productService.update(product);
    }
}
